package org.apache.flink.runtime.rest.handler.cluster;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.apache.flink.api.common.time.Time;
import org.apache.flink.runtime.resourceconsumer.ResourceConsumerGateway;
import org.apache.flink.runtime.rest.handler.HandlerRequest;
import org.apache.flink.runtime.rest.handler.RestHandlerException;
import org.apache.flink.runtime.rest.handler.resourceconsumer.AbstractResourceConsumerHandler;
import org.apache.flink.runtime.rest.messages.EmptyMessageParameters;
import org.apache.flink.runtime.rest.messages.EmptyRequestBody;
import org.apache.flink.runtime.rest.messages.MessageHeaders;
import org.apache.flink.runtime.rest.messages.ThreadDumpInfo;
import org.apache.flink.runtime.rpc.retriever.GatewayRetriever;
import org.apache.flink.runtime.webmonitor.RestfulGateway;

/** Rest handler which serves the thread dump info from the ConsumerManager. */
public class ConsumerManagerThreadDumpHandler
        extends AbstractResourceConsumerHandler<
                RestfulGateway, EmptyRequestBody, ThreadDumpInfo, EmptyMessageParameters> {

    public ConsumerManagerThreadDumpHandler(
            GatewayRetriever<? extends RestfulGateway> leaderRetriever,
            Time timeout,
            Map<String, String> responseHeaders,
            MessageHeaders<EmptyRequestBody, ThreadDumpInfo, EmptyMessageParameters> messageHeaders,
            GatewayRetriever<ResourceConsumerGateway> resourceConsumerGatewayRetriever
            ) {
        super(leaderRetriever, timeout, responseHeaders, messageHeaders, resourceConsumerGatewayRetriever);
    }

	@Override
	protected CompletableFuture<ThreadDumpInfo> handleRequest(HandlerRequest<EmptyRequestBody> request,
			ResourceConsumerGateway gateway) throws RestHandlerException {
        return gateway.requestThreadDump(timeout);
	}
}
